package cv.ltq.dao;

import cv.ltq.entiy.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentRepositroy {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<Student> getAll() {
        return jdbcTemplate.query("select * from student",new BeanPropertyRowMapper<>(Student.class));
    }


    public int add(Student student) {
        return jdbcTemplate.update("insert into student values (?,?,?)",
                student.getStudentname(),student.getGender(),student.getAge());
    }


    public int del(int id) {
        return jdbcTemplate.update("delete from student where id=?",id);
    }


    public int update(Student student) {
        return jdbcTemplate.update("update student set studentname=?,gender=?,age=? where id=?",
                student.getStudentname(),student.getGender(),student.getAge(),student.getId());
    }


    public Student getId(int id) {
        return jdbcTemplate.queryForObject("select * from student where id=?",new BeanPropertyRowMapper<>(Student.class),id);
    }
}
